import * as _ from 'lodash';
import { resolveReflectiveProviders } from '@angular/core/src/di/reflective_provider';
import { EsCommonService } from './escommon.service';
import { Injectable } from '@angular/core';

@Injectable()
export class WarnningService {
    now_datas: Array<any> = [
        {
            name: '10.12.21.122',
            status: 'stopping',
            sn: 'az001',
            cabinet: '1号柜',
            cpu: '4核',
            memory: '8GB',
            cpuuse: '50%',
            memoryuuse: '50%(4GB/8GB)',
            diskuse: '70%(70GB/100GB)',
            warn: 1,
            level: 'low',
            assignTo: '管理员',
            'releation': '-',
            reason: '监视进程exec.bat已关闭'
        },
        {
            name: '10.12.21.145',
            status: 'running',
            sn: 'az002',
            cabinet: '2号柜',
            cpu: '2核',
            memory: '4GB',
            cpuuse: '50%',
            memoryuuse: '50%(2GB/4GB)',
            diskuse: '70%(70GB/100GB)',
            warn: 1,
            level: 'mid',
            assignTo: '管理员',
            'releation': '-',
            reason: '监视进程java.exe已关闭'
        },
        {
            name: '10.12.21.155',
            status: 'running',
            sn: 'az003',
            cabinet: '2号柜',
            cpu: '2核',
            memory: '4GB',
            cpuuse: '50%',
            memoryuuse: '50%(2GB/4GB)',
            diskuse: '70%(70GB/100GB)',
            warn: 1,
            level: 'high',
            assignTo: '管理员',
            'releation': '-',
            reason: '物理机状态：down'
        },
        {
            name: '10.12.21.165',
            status: 'running',
            sn: 'az004',
            cabinet: '3号柜',
            cpu: '2核',
            memory: '4GB',
            cpuuse: '50%',
            memoryuuse: '50%(2GB/4GB)',
            diskuse: '70%(70GB/100GB)',
            warn: 1,
            level: 'mid',
            assignTo: '未分配',
            'releation': '-',
            reason: '监视进程java.exe已关闭'
        },
        {
            name: '10.12.21.180',
            status: 'running',
            sn: 'az005',
            cabinet: '3号柜',
            cpu: '2核',
            memory: '4GB',
            cpuuse: '80%',
            memoryuuse: '100%(4GB/4GB)',
            diskuse: '90%(90GB/100GB)',
            warn: 1,
            level: 'mid',
            assignTo: '未分配',
            'releation': '-',
            reason: '监视进程java.exe已关闭'
        },
        {
            name: '10.12.21.200',
            status: 'running',
            sn: 'az006',
            cabinet: '3号柜',
            cpu: '4核',
            memory: '4GB',
            cpuuse: '80%',
            memoryuuse: '100%(4GB/4GB)',
            diskuse: '90%(90GB/100GB)',
            warn: 1,
            level: 'high',
            assignTo: '未分配',
            'releation': '-',
            reason: '物理机状态：down'
        },
        {
            name: '10.12.21.210',
            status: 'running',
            sn: 'az007',
            cabinet: '3号柜',
            cpu: '4核',
            memory: '4GB',
            cpuuse: '80%',
            memoryuuse: '100%(4GB/4GB)',
            diskuse: '90%(90GB/100GB)',
            warn: 1,
            level: 'high',
            assignTo: '未分配',
            'releation': '-',
            reason: '物理机状态：down'
        },
        {
            name: '10.12.21.212',
            status: 'running',
            sn: 'az008',
            cabinet: '3号柜',
            cpu: '4核',
            memory: '4GB',
            cpuuse: '80%',
            memoryuuse: '100%(4GB/4GB)',
            diskuse: '90%(90GB/100GB)',
            warn: 1,
            level: 'high',
            assignTo: '未分配',
            'releation': '-',
            reason: '物理机状态：down'
        },
        {
            name: 'OA服务器001',
            ip: '10.12.21.122',
            status: 'stopping',
            network: '60700/120333',
            disk: '50%(50G/100G)',
            cpu: '30%',
            memory: '50%(2GB/4GB)',
            os: 'CentOS 7.2',
            warn: 10,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '未分配',
            'releation': 'OA服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: 'OA服务器002',
            ip: '10.12.21.145',
            status: 'running',
            network: '75000/150000',
            disk: '30%(30G/100G)',
            cpu: '30%',
            memory: '25%(1GB/4GB)',
            os: 'CentOS 7.2',
            warn: 11,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '未分配',
            'releation': 'OA服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: 'OA服务器003',
            ip: '10.12.21.155',
            status: 'running',
            network: '85000/10000',
            disk: '10%(10G/100G)',
            cpu: '30%',
            memory: '75%(3GB/4GB)',
            os: 'CentOS 7.2',
            warn: 10,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '未分配',
            'releation': 'OA服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: '代码库服务器001',
            ip: '10.12.21.165',
            status: 'running',
            network: '95000/150000',
            disk: '100%(100G/100G)',
            cpu: '50%',
            memory: '100%(4GB/4GB)',
            os: 'windows 2008',
            warn: 11,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '未分配',
            'releation': '代码库服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: '代码库服务器002',
            ip: '10.12.21.180',
            status: 'running',
            network: '105000/150000',
            disk: '20%(20G/100G)',
            cpu: '100%',
            memory: '100%(4GB/4GB)',
            os: 'windows 2008',
            warn: 11,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '未分配',
            'releation': '代码库服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: '代码库服务器003',
            ip: '10.12.21.200',
            status: 'running',
            network: '5000/150000',
            disk: '20%(20G/100G)',
            cpu: '20%',
            memory: '20%(1GB/4GB)',
            os: 'windows 2008',
            warn: 5,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '未分配',
            'releation': '代码库服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: '代码库服务器004',
            ip: '10.12.21.210',
            status: 'running',
            network: '75000/150000',
            disk: '15%(15G/100G)',
            cpu: '20%',
            memory: '20%(1GB/4GB)',
            os: 'windows 2008R2',
            warn: 1,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '未分配',
            'releation': '代码库服务器',
            reason: '守护进程keepAlve:关闭'
        }, {
            name: '部署服务器001',
            ip: '10.12.21.234',
            status: 'running',
            network: '75000/150000',
            disk: '80%(80G/100G)',
            cpu: '35%',
            memory: '50%(4GB/8GB)',
            os: 'Red Hat Linux 9',
            warn: 1,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '未分配',
            'releation': '部署服务器',
            reason: '守护进程keepAlve:关闭'
        },
    ];
    his_datas: Array<any> = [
        {
            name: '10.12.21.122',
            status: 'stopping',
            sn: 'az001',
            cabinet: '1号柜',
            cpu: '4核',
            memory: '8GB',
            cpuuse: '50%',
            memoryuuse: '50%(4GB/8GB)',
            diskuse: '70%(70GB/100GB)',
            warn: 1,
            level: 'low',
            assignTo: '管理员',
            'releation': '-',
            reason: '监视进程exec.bat已关闭'
        },
        {
            name: '10.12.21.145',
            status: 'running',
            sn: 'az002',
            cabinet: '2号柜',
            cpu: '2核',
            memory: '4GB',
            cpuuse: '50%',
            memoryuuse: '50%(2GB/4GB)',
            diskuse: '70%(70GB/100GB)',
            warn: 1,
            level: 'mid',
            assignTo: '管理员',
            'releation': '-',
            reason: '监视进程java.exe已关闭'
        },
        {
            name: '10.12.21.155',
            status: 'running',
            sn: 'az003',
            cabinet: '2号柜',
            cpu: '2核',
            memory: '4GB',
            cpuuse: '50%',
            memoryuuse: '50%(2GB/4GB)',
            diskuse: '70%(70GB/100GB)',
            warn: 1,
            level: 'high',
            assignTo: '管理员',
            'releation': '-',
            reason: '物理机状态：down'
        },
        {
            name: '10.12.21.165',
            status: 'running',
            sn: 'az004',
            cabinet: '3号柜',
            cpu: '2核',
            memory: '4GB',
            cpuuse: '50%',
            memoryuuse: '50%(2GB/4GB)',
            diskuse: '70%(70GB/100GB)',
            warn: 1,
            level: 'mid',
            assignTo: '管理员',
            'releation': '-',
            reason: '监视进程java.exe已关闭'
        },
        {
            name: '10.12.21.180',
            status: 'running',
            sn: 'az005',
            cabinet: '3号柜',
            cpu: '2核',
            memory: '4GB',
            cpuuse: '80%',
            memoryuuse: '100%(4GB/4GB)',
            diskuse: '90%(90GB/100GB)',
            warn: 1,
            level: 'mid',
            assignTo: '运维人员001',
            'releation': '-',
            reason: '监视进程java.exe已关闭'
        },
        {
            name: '10.12.21.200',
            status: 'running',
            sn: 'az006',
            cabinet: '3号柜',
            cpu: '4核',
            memory: '4GB',
            cpuuse: '80%',
            memoryuuse: '100%(4GB/4GB)',
            diskuse: '90%(90GB/100GB)',
            warn: 1,
            level: 'high',
            assignTo: '运维人员001',
            'releation': '-',
            reason: '物理机状态：down'
        },
        {
            name: '10.12.21.210',
            status: 'running',
            sn: 'az007',
            cabinet: '3号柜',
            cpu: '4核',
            memory: '4GB',
            cpuuse: '80%',
            memoryuuse: '100%(4GB/4GB)',
            diskuse: '90%(90GB/100GB)',
            warn: 1,
            level: 'high',
            assignTo: '运维人员001',
            'releation': '-',
            reason: '物理机状态：down'
        },
        {
            name: '10.12.21.212',
            status: 'running',
            sn: 'az008',
            cabinet: '3号柜',
            cpu: '4核',
            memory: '4GB',
            cpuuse: '80%',
            memoryuuse: '100%(4GB/4GB)',
            diskuse: '90%(90GB/100GB)',
            warn: 1,
            level: 'high',
            assignTo: '运维人员001',
            'releation': '-',
            reason: '物理机状态：down'
        },
        {
            name: 'OA服务器001',
            ip: '10.12.21.122',
            status: 'stopping',
            network: '60700/120333',
            disk: '50%(50G/100G)',
            cpu: '30%',
            memory: '50%(2GB/4GB)',
            os: 'CentOS 7.2',
            warn: 10,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '管理员',
            'releation': 'OA服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: 'OA服务器002',
            ip: '10.12.21.145',
            status: 'running',
            network: '75000/150000',
            disk: '30%(30G/100G)',
            cpu: '30%',
            memory: '25%(1GB/4GB)',
            os: 'CentOS 7.2',
            warn: 11,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '管理员',
            'releation': 'OA服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: 'OA服务器003',
            ip: '10.12.21.155',
            status: 'running',
            network: '85000/10000',
            disk: '10%(10G/100G)',
            cpu: '30%',
            memory: '75%(3GB/4GB)',
            os: 'CentOS 7.2',
            warn: 10,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '管理员',
            'releation': 'OA服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: '代码库服务器001',
            ip: '10.12.21.165',
            status: 'running',
            network: '95000/150000',
            disk: '100%(100G/100G)',
            cpu: '50%',
            memory: '100%(4GB/4GB)',
            os: 'windows 2008',
            warn: 11,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '管理员',
            'releation': '代码库服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: '代码库服务器002',
            ip: '10.12.21.180',
            status: 'running',
            network: '105000/150000',
            disk: '20%(20G/100G)',
            cpu: '100%',
            memory: '100%(4GB/4GB)',
            os: 'windows 2008',
            warn: 11,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '管理员',
            'releation': '代码库服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: '代码库服务器003',
            ip: '10.12.21.200',
            status: 'running',
            network: '5000/150000',
            disk: '20%(20G/100G)',
            cpu: '20%',
            memory: '20%(1GB/4GB)',
            os: 'windows 2008',
            warn: 5,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '管理员',
            'releation': '代码库服务器',
            reason: '守护进程keepAlve:关闭'
        },
        {
            name: '代码库服务器004',
            ip: '10.12.21.210',
            status: 'running',
            network: '75000/150000',
            disk: '15%(15G/100G)',
            cpu: '20%',
            memory: '20%(1GB/4GB)',
            os: 'windows 2008R2',
            warn: 1,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '管理员',
            'releation': '代码库服务器',
            reason: '守护进程keepAlve:关闭'
        }, {
            name: '部署服务器001',
            ip: '10.12.21.234',
            status: 'running',
            network: '75000/150000',
            disk: '80%(80G/100G)',
            cpu: '35%',
            memory: '50%(4GB/8GB)',
            os: 'Red Hat Linux 9',
            warn: 1,
            comment: '物理机服务器',
            level: 'high',
            assignTo: '管理员',
            'releation': '部署服务器',
            reason: '守护进程keepAlve:关闭'
        },
    ];
    constructor(private esCommonService: EsCommonService) {

    }
    queryProcessedWarnning(condition: any, show: String): any {
        let date: Date = new Date();
        date = new Date(this.esCommonService.getFormatDate(date, 'yyyy-MM-dd'));
        let startTime = null;
        let list = _.cloneDeep(this.his_datas);
        let useList = [];
        switch (show) {
            case 'today':
                startTime = date.getTime();
                for (let i = 0; i < list.length / 3; i++) {
                    useList.push(list[i]);
                }
                break;
            case 'three':
                startTime = date.getTime() - 3 * this.esCommonService.onDay;
                for (let i = 0; i < list.length / 1.5; i++) {
                    useList.push(list[i]);
                }
                break;
            case 'week':
                startTime = date.getTime() - 7 * this.esCommonService.onDay;
                useList = list;
                break;
        }

        for (let d of list) {
            d.startTime = startTime;
            switch (show) {
                case 'today':
                    d.endTime = Math.ceil(d.startTime + Math.random() * this.esCommonService.onDay);
                    break;
                case 'three':
                    d.endTime = Math.ceil(d.startTime + 3 * Math.random() * this.esCommonService.onDay);
                    break;
                case 'week':
                    d.endTime = Math.ceil(d.startTime + 7 * Math.random() * this.esCommonService.onDay);
                    break;
            }
            if (d.endTime > new Date().getTime()) {
                d.endTime = new Date().getTime();
            }
        }

        return this.queryMachine(condition, useList);
    }
    querynowWarnning(condition) {
        let date: Date = new Date();
        date = new Date(this.esCommonService.getFormatDate(date, 'yyyy-MM-dd'));
        let startTime = date.getTime();
        let list = _.cloneDeep(this.now_datas);
        for (let d of list) {
            d.startTime = startTime;
            d.endTime = Math.ceil(d.startTime + Math.random() * this.esCommonService.onDay);
            if (d.endTime > new Date().getTime()) {
                d.endTime = new Date().getTime();
            }
        }
        return this.queryMachine(condition, list);
    }

    queryMachine(condition, list) {
        let result = [];
        let findResult = [];
        if (!condition) {
            return list;
        }
        let search = condition.search;
        if (search != null) {
            for (let l of list) {
                if (l.name.indexOf(search) >= 0) {
                    result.push(l);
                }
            }
        } else {
            result = list;
        }

        if (condition.sort) {
            result = _.orderBy(result, [
                condition.sort.field
            ], [condition.sort.sort]);
        }

        let pageSize = condition.pageSize;
        if (pageSize == null) {
            pageSize = 10;
        }

        let pageNo = condition.pageNo;
        if (pageNo == null) {
            pageNo = 1;
        }

        for (let i = 0; i < result.length; i++) {
            if (i >= (pageNo - 1) * pageSize) {
                findResult.push(result[i]);
            }
            if (findResult.length >= pageSize) {
                break;
            }
        }

        return {
            data: findResult,
            totalCount: result.length
        };
    }
}